package com.phaeton.beemeup;

import java.util.HashMap;
import java.util.Map;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.ZoomCamera;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.background.SpriteBackground;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.util.GLState;
import org.andengine.util.debug.Debug;

import android.graphics.Typeface;

public class SceneManager {
	BeeMeUpActivity mActivity;
	Level mLevel;
	Camera mCamera;
	private BitmapTextureAtlas splashTextureAtlas;
	private TextureRegion splashTextureRegion;
	
	private BuildableBitmapTextureAtlas mBitmapTextureAtlas;
	private BuildableBitmapTextureAtlas mGUITextureAtlas;
	private BitmapTextureAtlas mBackgroundTexture;
	
	private ITextureRegion mBackgroundTextureRegion;
	private ITiledTextureRegion mBeeCombTextureRegion;
	private ITiledTextureRegion mHomeCombTextureRegion;
	protected ITiledTextureRegion mWaspCombTextureRegion;
	private ITiledTextureRegion mBeeTextureRegion;
	private ITiledTextureRegion mWaspTextureRegion;
	private ITiledTextureRegion mFlowerTextureRegion;
	private ITiledTextureRegion mSelectTextureRegion;
	private ITiledTextureRegion mFightTextureRegion;
	private ITiledTextureRegion mHealTextureRegion;
	private ITextureRegion mNewCombTextureRegion;

	private ITextureRegion mItemBarTextureRegion;
	private ITextureRegion mSpawnBeeTextureRegion;
	private ITextureRegion mSpawnBeeDeactivatedTextureRegion;
	private ITextureRegion mMenuTextureRegion;
	private ITextureRegion mEmptySlotTextureRegion;

	private ITextureRegion mBeeHUDTextureRegion;
	private ITextureRegion mCombHUDTextureRegion;	
	
	private Map<String, ITextureRegion> mTextures;
	private Map<String, ITiledTextureRegion> mTiledTextures;
	
	private Font mFont;
	
	private Scene splashScene;
	private Scene titleScene;
	private Scene levelMenuScene;
	private Scene mainGameScene;
	private SceneType currentScene;
	private TextureRegion mMMBGTextureRegion;
	private TextureRegion mMMButtonPlayTextureRegion;
	private TextureRegion mMMButtonSettingsTextureRegion;
	private TextureRegion mMMButtonCreditsTextureRegion;
	private BuildableBitmapTextureAtlas mMenuTextureAtlas;
	private TextureRegion mLevelMenuBGTextureRegion;
	private TextureRegion mLMButton1TextureRegion;
	
	public enum SceneType {
      SPLASH,
      TITLE,
      LEVELMENU,
      GAME
   }

	
	public SceneManager(BeeMeUpActivity pActivity, Camera pCamera){
		mActivity = pActivity;
		mCamera = pCamera;
		mTextures = new HashMap<String, ITextureRegion>();
		mTiledTextures = new HashMap<String, ITiledTextureRegion>();
	}
	
	//Method loads all of the splash scene resources
	public void loadSplashSceneResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		splashTextureAtlas = new BitmapTextureAtlas(mActivity.getTextureManager(), 256, 256, TextureOptions.DEFAULT);
		splashTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashTextureAtlas, mActivity,"splash/splash.png", 0, 0);
		splashTextureAtlas.load();
	}
	
	//Method loads all of the resources for the game scenes
	   public void loadGameSceneResources() {
			BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
			/* Load all the textures this game needs. */
			this.mBitmapTextureAtlas = new BuildableBitmapTextureAtlas(mActivity.getTextureManager(), 2048, 2048, TextureOptions.BILINEAR);
			
			this.mBeeCombTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, mActivity, "combs/bee.png", 6, 1);
			this.mTiledTextures.put("BeeComb", mBeeCombTextureRegion);
			
			this.mHomeCombTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, mActivity, "combs/bee.png", 6, 1);
			this.mTiledTextures.put("HomeComb", mHomeCombTextureRegion);
			
			this.mWaspCombTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, mActivity, "combs/wasp.png", 6, 1);
			this.mTiledTextures.put("WaspComb", mWaspCombTextureRegion);
			
			this.mBeeTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, mActivity, "bees/bee.png", 4, 2);
			this.mTiledTextures.put("Bee", mBeeTextureRegion);
			
			this.mWaspTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, mActivity, "wasps/wasp.png", 4, 2);
			this.mTiledTextures.put("Wasp", mWaspTextureRegion);
			
			this.mFlowerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, mActivity, "flowers/flower.png", 3, 5);
			this.mTiledTextures.put("Flower", mFlowerTextureRegion);
			
			this.mSelectTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, mActivity, "fx/select.png", 4, 4);
			this.mTiledTextures.put("Select", mSelectTextureRegion);
			
			this.mNewCombTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, mActivity, "combs/newBeeComb.png");
			this.mTextures.put("NewComb", mNewCombTextureRegion);
			
			this.mFightTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, mActivity, "fight/fight.png", 2, 1);
			this.mTiledTextures.put("Fight", mFightTextureRegion);

			this.mHealTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, mActivity, "fx/heal.png", 2, 1);
			this.mTiledTextures.put("Heal", mHealTextureRegion);
			
			//GUI
			this.mGUITextureAtlas = new BuildableBitmapTextureAtlas(mActivity.getTextureManager(), 1024, 1024, TextureOptions.NEAREST);
			
			this.mItemBarTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mGUITextureAtlas, mActivity, "gui/ingame_bar.png");
			this.mTextures.put("ItemBar", mNewCombTextureRegion);
			
			this.mSpawnBeeTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mGUITextureAtlas, mActivity, "gui/ingame_button_spawnbee.png");
			this.mTextures.put("SpawnBee", mSpawnBeeTextureRegion);
			
			this.mSpawnBeeDeactivatedTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mGUITextureAtlas, mActivity, "gui/ingame_button_spawnbee_deactivated.png");
			this.mTextures.put("SpawnBeeDeactivated", mSpawnBeeDeactivatedTextureRegion);
			
			this.mMenuTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mGUITextureAtlas, mActivity, "gui/ingame_button_menu.png");
			this.mTextures.put("Menu", mMenuTextureRegion);
			
			this.mEmptySlotTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mGUITextureAtlas, mActivity, "gui/ingame_button_bg.png");
			this.mTextures.put("EmptySlot", mEmptySlotTextureRegion);
			
			this.mBeeHUDTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mGUITextureAtlas, mActivity, "gui/beeHUDIcon.png");
			this.mTextures.put("BeeHUD", mBeeHUDTextureRegion);
			
			this.mCombHUDTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mGUITextureAtlas, mActivity, "gui/combHUDIcon.png");
			this.mTextures.put("CombHUD", mCombHUDTextureRegion);
			
			this.mMenuTextureAtlas = new BuildableBitmapTextureAtlas(mActivity.getTextureManager(), 2048, 1024, TextureOptions.NEAREST);
			
			this.mMMBGTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTextureAtlas, mActivity, "gui/MainMenu.png");
			this.mTextures.put("MMBG", mMMBGTextureRegion);
			
			this.mMMButtonPlayTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTextureAtlas, mActivity, "gui/button_mm_play.png");
			this.mTextures.put("ButtonPlay", mMMButtonPlayTextureRegion);
			
			this.mMMButtonSettingsTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTextureAtlas, mActivity, "gui/button_mm_settings.png");
			this.mTextures.put("ButtonSettings", mMMButtonSettingsTextureRegion);
			
			this.mMMButtonCreditsTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTextureAtlas, mActivity, "gui/button_mm_credits.png");
			this.mTextures.put("ButtonCredits", mMMButtonCreditsTextureRegion);
			
			this.mLevelMenuBGTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTextureAtlas, mActivity, "gui/LevelMenu.png");
			this.mTextures.put("LevelMenu", mLevelMenuBGTextureRegion);
			
			this.mLMButton1TextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTextureAtlas, mActivity, "gui/button_lm_l1.png");
			this.mTextures.put("ButtonLevel1", mLMButton1TextureRegion);
			
			
			this.mBackgroundTexture = new BitmapTextureAtlas(mActivity.getTextureManager(), 256, 256, TextureOptions.REPEATING_BILINEAR);
			this.mBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBackgroundTexture, mActivity, "backgrounds/bg.png", 0, 0);
			this.mTextures.put("Background", mBackgroundTextureRegion);
			this.mBackgroundTexture.load();
	//
			this.mFont = FontFactory.create(mActivity.getFontManager(), mActivity.getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32);
			this.mFont.load();
			/* Load all the sounds this game needs. */
//			try {
//				SoundFactory.setAssetBasePath("mfx/");
//				this.mGameOverSound = SoundFactory.createSoundFromAsset(this.getSoundManager(), this, "game_over.ogg");
//				this.mMunchSound = SoundFactory.createSoundFromAsset(this.getSoundManager(), this, "munch.ogg");
//			} catch (final IOException e) {
//				Debug.e(e);
//			}
			
			/* Textures hinzufuegen */
			try {
				this.mBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 1));
				this.mBitmapTextureAtlas.load();
				
				this.mGUITextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 1));
				this.mGUITextureAtlas.load();				

				this.mMenuTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 1));
				this.mMenuTextureAtlas.load();
			} catch (TextureAtlasBuilderException e) {
				Debug.e(e);
			}
	   }


	//Method creates the Splash Scene
	public Scene createSplashScene() {
		//Create the Splash Scene and set background colour to red and add the splash logo.
		splashScene = new Scene();
		splashScene.setBackground(new Background(1, 0, 0));
		Sprite splash = new Sprite(0, 0, splashTextureRegion, mActivity.getVertexBufferObjectManager())
		{
			@Override
			protected void preDraw(GLState pGLState, Camera pCamera)
			{
				super.preDraw(pGLState, pCamera);
				pGLState.enableDither();
			}
		};
		splash.setScale(1.5f);
		splash.setPosition((mCamera.getWidth() - splash.getWidth()) * 0.5f, (mCamera.getHeight() - splash.getHeight()) * 0.5f);
		splashScene.attachChild(splash);

		return splashScene;
	}

	//Method creates all of the Game Scenes
	public void createTitleScene() {
		//Create the Title Scene and set background colour to green
		titleScene = new Scene();
		titleScene.setBackground(new SpriteBackground(new Sprite(0,0,this.mMMBGTextureRegion,mActivity.getVertexBufferObjectManager())));
		Sprite buttonPlay = new Sprite(mCamera.getWidth() / 2 - this.mMMButtonPlayTextureRegion.getWidth() / 2, 100, mMMButtonPlayTextureRegion, mActivity.getVertexBufferObjectManager()){
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				 setGameScene(levelMenuScene);
		         setCurrentScene(SceneType.LEVELMENU);	
		         return true;
			}
		};
		Sprite buttonSettings = new Sprite(mCamera.getWidth() / 2 - this.mMMButtonSettingsTextureRegion.getWidth() / 2, 200, mMMButtonSettingsTextureRegion, mActivity.getVertexBufferObjectManager()){
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				return false;			
			}
		};
		Sprite buttonCredits = new Sprite(mCamera.getWidth() / 2 - this.mMMButtonCreditsTextureRegion.getWidth() / 2, 300, mMMButtonCreditsTextureRegion, mActivity.getVertexBufferObjectManager()){
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				return false;			
			}
		};
		titleScene.setTouchAreaBindingOnActionDownEnabled(true);
		titleScene.attachChild(buttonPlay);
		titleScene.attachChild(buttonSettings);
		titleScene.attachChild(buttonCredits);
		titleScene.registerTouchArea(buttonPlay);
		titleScene.registerTouchArea(buttonSettings);
		titleScene.registerTouchArea(buttonCredits);
	}
	
	public void createLevelMenuScene() {
		//Create the Title Scene and set background colour to green
		levelMenuScene = new Scene();
		levelMenuScene.setBackground(new SpriteBackground(new Sprite(0,0,this.mLevelMenuBGTextureRegion,mActivity.getVertexBufferObjectManager())));
		Sprite buttonL1 = new Sprite(140, 90, mLMButton1TextureRegion, mActivity.getVertexBufferObjectManager()){
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				 setGameScene(mActivity.getGameManager().loadLevel("testLevel.xml"));
		         setCurrentScene(SceneType.GAME);	
		         return true;			
			}
		};
		levelMenuScene.setTouchAreaBindingOnActionDownEnabled(true);
		levelMenuScene.attachChild(buttonL1);
		levelMenuScene.registerTouchArea(buttonL1);
	}

	public void setGameScene(Scene scene){

		//Create the Main Game Scene and set background colour to blue
		mainGameScene = scene;
		
	}
	
	//Method allows you to set the currently active scene
	public void setCurrentScene(SceneType scene) {
		currentScene = scene;
		switch (scene)
		{
		case SPLASH:
			break;
		case TITLE:
			mActivity.getEngine().setScene(titleScene);
			break;
		case LEVELMENU:
			mActivity.getEngine().setScene(levelMenuScene);
			break;
		case GAME:
			mActivity.getEngine().setScene(mainGameScene);
			break;
		}
	}
	
	//Method allows you to get the currently active scene
   public SceneType getCurrentScene() {
      return currentScene;
   }
   
   public ITextureRegion getTexture(String key){
	   return mTextures.get(key);
   }

   public ITiledTextureRegion getTiledTexture(String key){
	   return mTiledTextures.get(key);
   }
   
   public Font getFont(){
	   return mFont;
   }

}
